/*
势力配置
name 势力名称
boss 主公
job  势力存在的职业（初级职业）
citys 势力占领的城池
flag  true--官府  false--山贼
color 势力颜色
condition 加入声望门槛
*/
window._MASTER = {

   
}

window._MASTERHELPER = {

    testAllMaster:function(){

       var masterObj = _MASTERHELPER.getMasterObj();
       console.log('武将分布',masterObj);

    },


    getMasterObj:function(){

        var masterObj = {'中立':{}};
        var areaMap = {};

        for(var key in _MASTER){

            masterObj[key]={};

            var citys = _MASTER[key].citys;

            for(var j=0;j<citys.length;j++){
                var city = citys[j];
                if(_AREAARMY[city]){
                    areaMap[city]=1;
                    var guardArmy = _AREAARMY[city].guard;
                    var bossArmy = _AREAARMY[city].boss;

                    for(var i=0;i<guardArmy.length;i++){
                        masterObj[key][guardArmy[i]]=city;
                    }

                    for(var i=0;i<bossArmy.length;i++){
                        if(!masterObj[key][bossArmy[i]]){
                            masterObj['中立'][bossArmy[i]]=city;
                        }
                    }
                }else{
                    console.log(key+" "+city+" 不存在");
                }

            }

        }

        //没有归属的城池 守卫和boss都属于中立
        for(var key in _AREAARMY){
            if(!areaMap[key]){
                var guardArmy = _AREAARMY[key].guard;
                var bossArmy = _AREAARMY[key].boss;
                for(var i=0;i<guardArmy.length;i++){
                    masterObj['中立'][guardArmy[i]]=key;
                }
                for(var i=0;i<bossArmy.length;i++){
                    masterObj['中立'][bossArmy[i]]=key;
                }
            }
        }

        for(var key in masterObj){

            var arr = [];
            for(var akey in masterObj[key]){
                arr.push({name:akey,city:masterObj[key][akey]});
            }
            masterObj[key]=arr;

        }

        return masterObj;

    },


    initNpcLog:function(){

        var masterObj = _MASTERHELPER.getMasterObj();
        var npclog = {};
        for(var key in masterObj){
            var npcArr = masterObj[key];
            for(var i=0;i<npcArr.length;i++){
                var tempnpc = npcArr[i];
                var tempNpclog = _CU.cloneObj(_GAMEDATAHELPER._defaultNpcLog);
                tempNpclog.country = key;
                tempNpclog.city = tempnpc.city;
                npclog[tempnpc.name]=tempNpclog;
            }

        }

        return npclog;

    },

    //初始化城池归属
    initCityLog:function(){

        var cityLog = {};
        for(var key in _MASTER){

            var cityarr = _MASTER[key].citys;

            for(var i=0;i<cityarr.length;i++){

                cityLog[cityarr[i]]=key;

            }

        }

        return cityLog;

    },


    //根据城池所在势力获取该势力下属小怪的职业
    getJobsOfCity:function(cityname){

        if(
            window.gameData
            &&window.gameData.cityLog
            &&window.gameData.cityLog[cityname]
            ){

            var master = window.gameData.cityLog[cityname];
            if(_MASTER[master]){
                return _MASTER[master].job;
            }

        }

        return null;

    },

    getMasterOfCity:function(cityname){

        if(
            window.gameData
            &&window.gameData.cityLog
            &&window.gameData.cityLog[cityname]
            ){

            return window.gameData.cityLog[cityname];

        }

        return null;

    },

    getCountryPlayer:function(country){
        var playernmb = 0;
        for(var key in window.gameData.realPos){
            var npclog = window.gameData.realPos[key];
            if(npclog.country==country&&!npclog.die){
                if(
                    !npclog.zm
                    ||(window.gameData.country&&window.gameData.country==country)
                ){
                    playernmb++;
                }
            }
        }
        return playernmb;
    },

    isCountryOver:function(country){

        if(
            window.gameData
            &&window.gameData.cityLog
            ){

            for(var key in window.gameData.cityLog){
                if(window.gameData.cityLog[key]==country){
                    return false;
                }
            }

        }

        return true;
    },

    getRoleNmbByMaster:function(master){

        if(
            window.gameData
            &&window.gameData.realPos
        ){
            var nmb=0;
            for(var key in window.gameData.realPos){
                if(window.gameData.realPos[key].country==master&&!window.gameData.realPos[key].die)
                    nmb++;
            }
            return nmb;

        }

        return 0;

    },


    getCityNmbByMaster:function(master){

        if(
            window.gameData
            &&window.gameData.cityLog
        ){
            var nmb=0;
            for(var key in window.gameData.cityLog){
                if(window.gameData.cityLog[key]==master)
                nmb++;
            }
            return nmb;

        }

        return 0;

    },
	
	//获取当前势力老大名字
	getRealMasterBoss:function(masterName){
		var masterConf = _MASTER[masterName];
		if(masterConf){
			var boosname = masterConf.boss;
				
			if(
				window.gameData.country==masterName
				&&window.gameData.isMaster
			){
				boosname = window.gameData.playerTeam[0].name;
			}
			
			return boosname;
		}
		return '未知';
	},

    getMasterIntro:function(masterName){

        //console.log(masterName,_MASTER[masterName]);

        if(_MASTER[masterName]){
			
            var masterConf = _MASTER[masterName];
            var cityNmb = _MASTERHELPER.getCityNmbByMaster(masterName);
            var result = "势力:"+masterConf.name;
            result+="\n主公:"+_MASTERHELPER.getRealMasterBoss(masterName);
            result+="\n类型:"+(masterConf.flag?'官府':'山贼');
            result+="\n声望:"+(masterConf.condition?masterConf.condition:'无');
            result+="\n人员:"+_MASTERHELPER.getRoleNmbByMaster(masterName);
            result+="\n城池:"+cityNmb;
            result+="\n加成:\n"+_IOF.getAttrString(masterConf.excel);
            result+="状态:"+(cityNmb>0?'存在':'灭亡');
            return result;
        }

        return "未知";

    }

}